/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package example.hello;

import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.PathParam;
import javax.ws.rs.Consumes;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.enterprise.context.RequestScoped;

/**
 * REST Web Service
 *
 * @author doo
 */
@Path("generic")
@RequestScoped
public class InfoRessource {

    @Context
    private UriInfo context;

    /**
     * Creates a new instance of InfoRessource
     */
    public InfoRessource() {
    }

    /**
     * Retrieves representation of an instance of example.hello.InfoRessource
     *
     * @return an instance of java.lang.String
     */
    @GET
    @Produces("application/xml")
    @Path("/{server}/{requestType}")
    public Object getXml(@PathParam("server") String server, @PathParam("requestType") String request) throws NotBoundException, RemoteException, MalformedURLException, SQLException {
        //TODO return proper representation object

        ArrayList<String> liste = null;
        String memoire = null;
        String cpu = null;


        Monitoring m = (Monitoring) Naming.lookup("//localhost/Monitoring");
        SystemInfo display = m.quid();
        System.out.println("ip du serveur : " + display.getIp().getIp());

        if (request.equals("prog")) {
            /*
             * test
             */
            InterfaceSQL requete = new InterfaceSQL();
            String insert_processus = "INSERT INTO monito.grossetable (nom,methode,resultat) VALUES('" + display.getIp().getIp() + "', 'Processus'" + ",'" + display.getProg().getProg() + "');";
            requete.sendReq(InterfaceSQL.TypeReq.update, insert_processus);
            requete.closeAll();
            /*
             * fin test
             */
            return display.getProg();

        }

        if (request.equals("ram")) {
            InterfaceSQL requete = new InterfaceSQL();
            String insert_memoire = "INSERT INTO monito.grossetable (nom,methode,resultat) VALUES('" + display.getIp().getIp() + "', 'ram'" + ",'" + display.getRam().getRam() + "');";
            requete.sendReq(InterfaceSQL.TypeReq.update, insert_memoire);
            requete.closeAll();
            return display.getRam();

        }

        if (request.equals("cpu")) {

            InterfaceSQL requete = new InterfaceSQL();
                    String insert_cpu = "INSERT INTO monito.grossetable (nom,methode,resultat) VALUES('"+display.getIp().getIp()+"', 'CPU'"+",'"+display.getCpu().getCpu()+"');";
//                    String insert_cpu = "INSERT INTO monito.grossetable (nom,methode,resultat) VALUES ('toto', 'toto', 'toto');";
//                    System.out.println(insert_cpu);

                    requete.sendReq(InterfaceSQL.TypeReq.update, insert_cpu);
                    requete.closeAll();
//            System.out.println(display.getCpu().getCpu());
            return display.getCpu();

        }


        return "Requete inconnue";

    }

    /**
     * PUT method for updating or creating an instance of InfoRessource
     *
     * @param content representation for the resource
     * @return an HTTP response with content of the updated or created resource.
     */
    @PUT
    @Consumes("application/xml")
    public void putXml(String content) {
    }
}
